<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <!-- 图标 -->
      <img class="login-logo" src="@/assets/logo.595745bd.png" alt="">
      <!-- 账号输入框 -->
      <el-form-item prop="loginName">
        <span class="svg-container el-icon-mobile-phone" />
        <el-input
          ref="loginName"
          v-model="loginForm.loginName"
          placeholder="请输入账号"
          name="loginName"
          type="text"
        />
      </el-form-item>
      <!-- 密码输入框 -->
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <!-- 验证码输入框 -->
      <el-form-item prop="code">
        <div class="yzm">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="code"
            v-model="loginForm.code"
            placeholder="请输入验证码"
            name="code"
          />
          <div><img :src="img" alt=""></div>
        </div>
      </el-form-item>

      <el-button v-loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click="subForm">登录</el-button>

    </el-form>
  </div>
</template>

<script>
import { getImgCodeAPI } from '@/api/user'
import { RandomString } from '@/utils/validate'
export default {
  name: 'Login',
  data() {
    return {
      loading: false,
      img: '',
      // 这么写的好处是，接口请求的时候直接把loginForm传过去就行了，牛逼
      loginForm: {
        loginName: '',
        password: '',
        code: '',
        clientToken: RandomString(),
        loginType: 0
      },
      loginRules: {
        loginName: [{ required: true, trigger: 'blur', message: '请输入账号' }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }],
        code: [{ required: true, trigger: 'blur', message: '请输入验证码' }]
      },
      passwordType: 'password'

    }
  },
  created() {
    this.getImgCode()
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    async getImgCode() {
      const res = await getImgCodeAPI(this.loginForm.clientToken)
      const url = res.request.responseURL
      this.img = url
    },
    subForm() {
      // console.log(this.$refs.loginForm)拿到的是form表单这个封装好的组件，组件的本质是一个名为VueComponent的构造函数。根据文档可以使用上面的方法。
      this.$refs.loginForm.validate(async(vali) => {
        if (vali) {
          this.loading = true
          try {
            await this.$store.dispatch('user/loginAction', this.loginForm)
            this.$router.push('/')
          } finally {
            this.loading = false
          }
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #999999;
      height: 47px;
      caret-color: #999999;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px black inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    width: 100%;
    height: 52px;
    margin-bottom: 24px;
    background: #fff;
    border: 1px solid #e2e2e2;
    border-radius: 4px
  }
  .yzm{
    display: flex;
    line-height: 40px;
    position: relative;
    font-size: 14px;
    margin-bottom: -12px;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  position: relative;
    height: 100%;
    width: 100%;
    overflow: hidden;
    background-repeat: no-repeat;
    background-size: cover;
  background-image: url(http://likede2-admin.itheima.net/img/background.be4fae7d.png);

  .login-form {
    position: absolute;
    width: 518px;
    height: 388px;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 76px 35px 0;
    background: #fff;
    box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    border-radius: 10px;
  }

  .tips {
    font-size: 14px;
    color: red;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .login-logo{
        position: absolute;
    width: 96px;
    height: 96px;
    top: -46px;
    left: 50%;
    margin-left: -48px;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
